package com.example.administrator.idlereader.news.model;


import com.example.administrator.idlereader.bean.NewsBean;
import com.example.administrator.idlereader.http.Api;
import com.example.administrator.idlereader.http.RetrofitHelper;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func2;
import rx.schedulers.Schedulers;
public class NewsModel implements INewsModel {

    @Override
    public void loadNews(final String hostType, final int startPage, final String id,
                         final INewsLoadListener iNewsLoadListener) {
        RetrofitHelper retrofitHelper = new RetrofitHelper(Api.NEWS_HOST);
        retrofitHelper.getNews(hostType, id, startPage)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
               .subscribe(new Subscriber<NewsBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                iNewsLoadListener.fail(e);
            }

            @Override
            public void onNext(NewsBean newsBean) {
                if (startPage != 0) {
                    iNewsLoadListener.loadMoreSuccess(newsBean);
                } else {
                    iNewsLoadListener.success(newsBean);
                }

            }
        });
    }
}
